package com.htj.powerbank.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.htj.powerbank.controller.vo.State;
import com.htj.powerbank.entity.User;
import com.htj.powerbank.service.UserService;
import com.htj.powerbank.service.User_pmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@Api(value = "登录",tags = {"用户接口"})
public class UserController {
    
    @Autowired
    UserService userService;

    @Autowired
    User_pmService user_pmService;

    @ApiOperation(value = "用户查询")//swagger接口信息备注
    @GetMapping( "/list")
    public R getUser(){
        return R.ok(user_pmService.getUser_pm());
    }

    @GetMapping( "/byid")
    public R userbyid(){
        return R.ok(user_pmService.getUser_pm());
    }

    //实现注册功能
    @ApiOperation(value = "用户注册，新增")//swagger接口信息备注
    @PostMapping ( "/register")
    public R register(@RequestBody User user){//post专用
        JSONObject jsonObject = new JSONObject();
//        System.out.println(user);
        if(user.getName() != null && user.getPassword() != null) {

            //通过用户名查询该用户
            User one = userService.getUserByName(user.getName());
//            System.out.println(one);
            //用户存在
            if(one != null) {
                jsonObject.put("code",201);
                jsonObject.put("msg","注册失败，用户名已存在");
                return R.ok(jsonObject);
            }
            else
            {
                int check = userService.addUser(user);
                System.out.println(check);
                if(check==1) {
                    jsonObject.put("code", 200);
                    jsonObject.put("msg","注册成功");
                    return R.ok(jsonObject);
                }
            }
        }

        jsonObject.put("code", 202);
        jsonObject.put("msg", "其他错误");
        return R.ok(jsonObject);
    }

    @ApiOperation("存储token")
    @GetMapping("/info")
    public R lstoken(String token){
        State state = new State();
        state.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        state.setName("Super Admin");
//        state.setRoles("admin");
        state.setRoles(token);
        state.setIntroduction("I am a super administrator");
        return R.ok(state);
    }

    @ApiOperation(value = "登录")//swagger接口信息备注
    @PostMapping("/logout")
    public R logout() {
        JSONObject jsonObject = new JSONObject();
        return R.ok(jsonObject);
    }

    @ApiOperation(value = "登录")//swagger接口信息备注
    @PostMapping("/login")
    public R login(@RequestBody User user) {
        JSONObject jsonObject = new JSONObject();
        //用户名密码不为空
        if(user.getName() != null && user.getPassword() != null) {

            //通过用户名查询该用户
            User one = userService.getUserByName(user.getName());

            //用户存在 用户没有注销
            if(one != null) {
//                System.out.println(one.toString());

                //用户名,密码正确
                if(one.getName().equals(user.getName())  && one.getPassword().equals(user.getPassword())) {
                    jsonObject.put("code", 200);
                    jsonObject.put("user", JSONObject.parseObject(one.toString()));
                    jsonObject.put("token","admin");
                    jsonObject.put("msg", "登入成功!");
                    return R.ok(jsonObject);
                }
            }
        }
        //其他各种错误的处理
        jsonObject.put("code", 201);
        jsonObject.put("msg", "账户或密码错误!");
        return R.ok(jsonObject);
    }
}